package com.acme.entity;

import com.acme.entity.util.ChangeSupportList;
import com.acme.entity.util.EntityBean;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.swing.event.ChangeListener;

/**
 *
 * @author alejandro
 */
@Entity
@Table(name = "EMPLEADO", catalog = "carreteras", schema = "dbo")
@NamedQueries({
    @NamedQuery(name = "Empleado.findAll", query = "SELECT e FROM Empleado e"),
    @NamedQuery(name = "Empleado.findByNumero", query = "SELECT e FROM Empleado e WHERE e.numero = :numero")})
public class Empleado extends EntityBean<Empleado> {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "numero", nullable = false)
    private Integer numero;
    @JoinColumn(name = "contacto", referencedColumnName = "id", nullable = false)
    @ManyToOne(optional = false)
    private Contacto contacto;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "empleado")
    private final ChangeSupportList<Caseta> casetaList = new ChangeSupportList<Caseta>();
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "empleado")
    private final ChangeSupportList<Tramo> tramoList = new ChangeSupportList<Tramo>();

    public Empleado() {
    }

    public Empleado(Integer numero) {
        this.numero = numero;
    }

    public Empleado(Integer numero, Contacto contacto, List<Caseta> casetaList, List<Tramo> tramoList) {
        this.numero = numero;
        this.contacto = contacto;
        mergeListContent(this.casetaList, casetaList);
        mergeListContent(this.tramoList, tramoList);
    }

    public Integer getNumero() {
        return numero;
    }

    public void setNumero(Integer numero) {
        this.numero = numero;
        support.fireChange();
    }

    public Contacto getContacto() {
        return contacto;
    }

    public void setContacto(Contacto contacto) {
        this.contacto = contacto;
        support.fireChange();
    }

    public List<Caseta> getCasetaList() {
        return casetaList;
    }

    public void setCasetaList(List<Caseta> casetaList) {
        mergeListContent(this.casetaList, casetaList);
//        support.fireChange(); <-- La lista ya controla los cambios
    }

    public List<Tramo> getTramoList() {
        return tramoList;
    }

    public void setTramoList(List<Tramo> tramoList) {
        mergeListContent(this.tramoList, tramoList);
//        support.fireChange(); <-- La lista ya controla los cambios
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (numero != null ? numero.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Empleado)) return false;
        Empleado other = (Empleado) object;
        if ((this.numero == null && other.numero != null) || (this.numero != null && !this.numero.equals(other.numero))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        if( contacto!=null ) return String.format("[%s] %s", numero, contacto);
        return String.format("[%s] desconocido", numero);
    }

    @Override
    public void addChangeListener(ChangeListener listener) {
        tramoList.addChangeListener(listener);
        casetaList.addChangeListener(listener);
        super.addChangeListener(listener);
    }

    @Override
    public Empleado clone() {
        return new Empleado(numero, contacto, casetaList, tramoList);
    }

    @Override
    public void merge(Empleado other) {
        this.numero = other.numero;
        this.contacto = other.contacto;
        mergeListContent(this.casetaList, other.casetaList);
        mergeListContent(this.tramoList, other.tramoList);
    }

}
